package com.huiquan.synonymy.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.foundation.constant.TabID;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.synonymy.domain.Synonymy;
import com.huiquan.synonymy.service.SynonymyService;
import com.huiquan.vocab.service.VocabElementRService;
import com.huiquan.vocab.service.VocabElementTService;

@Controller
public class SynonymyController {

	@Autowired
	private SynonymyService synonymyService;
	@Autowired
	private VocabElementTService vocabElementTService;
	@Autowired
	private VocabElementRService vocabElementRService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/synonymy/synonymy_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		int type;
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);

			type = Synonymy.TYPE_ELEMENT;
			if (TabID.SYNONYMY_SLANG.idEquals(tabid)) {
				type = Synonymy.TYPE_SLANG;
			}
			pageParam.put("type", String.valueOf(type));
			pageParam.put("start", "1");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
			type = Integer.parseInt(pageParam.get("type").toString());
		}

		// 获取页面参数，如果request中有值则修改session，否则从session中拿值
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String wordKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "wordKey");
		String propertyKeyStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "propertyKey");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return synonymyService.list(start, wordKey, propertyKeyStr, type, exactSearchFlag, countPerPageStr);
	}

	@RequestMapping(value = "/synonymy/synonymy_pre_edit.htm")
	public ModelAndView preEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		int type = Integer.parseInt(RequestParamUtil.getParamBySession(req, "type"));

		return synonymyService.preEdit(id, type);
	}

	@ResponseBody
	@RequestMapping(value = "/synonymy/synonymy_edit.do")
	public ReturnData synonymyEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String std = req.getParameter("std");
		String englishStd = req.getParameter("englishStd");
		String[] words = req.getParameterValues("word");
		String property = req.getParameter("property");
		User user = (User) req.getSession().getAttribute("user");
		int type = Integer.parseInt(RequestParamUtil.getParamBySession(req, "type"));

		return synonymyService.edit(type, id, property, words, user, std, englishStd, true);
	}

	@ResponseBody
	@RequestMapping(value = "/synonymy/synonymy_edit_batch.do")
	public ReturnData synonymyEditBatch(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		int type = Integer.parseInt(RequestParamUtil.getParamBySession(req, "type"));

		List<Map<String, Object>> params = new ArrayList<>();
		int i = 0;
		String suffix = "";
		boolean flag = true;
		StringBuffer ids = new StringBuffer();
		while (flag) {
			String id = req.getParameter("id" + suffix);
			if (id != null && !id.isEmpty()) {
				ids.append(id).append(",");
			}
			String[] words = req.getParameterValues("word" + suffix);
			String englishStd = req.getParameter("englishStd" + suffix);
			Map<String, Object> param = new HashMap<>();
			param.put("id", id);
			param.put("words", words);
			param.put("englishStd", englishStd);
			if (Synonymy.TYPE_ELEMENT.equals(type)) {
				param.put("property", req.getParameter("property" + suffix));
				param.put("std", req.getParameter("std" + suffix));
			}
			params.add(param);

			i++;
			suffix = "_" + i;
			flag = req.getParameter("id" + suffix) != null && !req.getParameter("id" + suffix).isEmpty();
		}
		ids.deleteCharAt(ids.length() - 1);
		User user = (User) req.getSession().getAttribute("user");

		return synonymyService.editBatch(type, params, ids.toString(), user);
	}

	@ResponseBody
	@RequestMapping(value = "/synonymy/synonymy_delete.do")
	public ReturnData delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		return synonymyService.delete(req.getParameter("id"));
	}

	@ResponseBody
	@RequestMapping(value = "/synonymy/synonymy_merge.do")
	public ReturnData merge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String[] ids = req.getParameterValues("id_box");
		User user = (User) req.getSession().getAttribute("user");

		return synonymyService.merge(ids, user);
	}

	@ResponseBody
	@RequestMapping(value = "/synonymy/synonymy_import.do")
	public ReturnData synonymImport(HttpServletRequest req, @RequestParam(required = false) MultipartFile file)
			throws Exception {
		User user = (User) req.getSession().getAttribute("user");
		String property = req.getParameter("property");
		int type = Integer.parseInt(RequestParamUtil.getParamBySession(req, "type"));

		return synonymyService.synonymImport(type, file, user, property);
	}

	@RequestMapping(value = "/synonymy/synonymy_export.htm")
	public void synonymExport(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String property = req.getParameter("property");
		int type = Integer.parseInt(RequestParamUtil.getParamBySession(req, "type"));
		String no = req.getParameter("no");

		synonymyService.synonymExport(type, property, resp, no);
	}

	@ResponseBody
	@RequestMapping(value = "/synonymy/change_std.do")
	public ReturnData changeStd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String word = req.getParameter("word");
		String property = req.getParameter("property");
		User user = (User) req.getSession().getAttribute("user");

		return synonymyService.changeStd(id, word, property, user);
	}

	@ResponseBody
	@RequestMapping(value = "/synonymy/change_english_std.do")
	public ReturnData changeEnglishStd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String word = req.getParameter("word");
		String property = req.getParameter("property");
		User user = (User) req.getSession().getAttribute("user");

		return synonymyService.changeEnglishStd(id, word, property, user);
	}

	@ResponseBody
	@RequestMapping(value = "/synonymy/element_t_init.do")
	public ReturnData elementTInit(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		vocabElementTService.initTSynonymy();

		return ReturnUtil.success();
	}

	@ResponseBody
	@RequestMapping(value = "/synonymy/init_r_synonymy.do")
	public ReturnData initRSynonymy(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		return vocabElementRService.initRSynonymy();
	}
	
	public static void main(String[] args) {
		System.out.println("a;b，c".replaceAll("[;，]", "%"));
	}
}
